package ru.amse.tsyganov.jumleditor.model.common;



public abstract class Edge extends ModelElement {
	
	private StateVertex source;
	
	private StateVertex target;
	
	public Edge(
			StateVertex sourceVertex, 
			StateVertex targetVertex) {
		
		if (sourceVertex == null || targetVertex == null) {
			throw new IllegalArgumentException();
		}
		
		source = sourceVertex;
		target = targetVertex;
	}
	
	public StateVertex getSource() {
		assert source != null;
		return source;
	}

	public void setSource(StateVertex sourceVertex) {
		
		if (sourceVertex == null) {
			throw new IllegalArgumentException();
		}
		
		assert source.containsOutgoingTransition(this);
		
		source.removeOutgoingTransition(this);
		source = sourceVertex;
		source.addOutgoingTransition(this);
		
		assert source.containsOutgoingTransition(this);
	}

	public StateVertex getTarget() {
		assert target != null;
		return target;
	}

	public void setTarget(StateVertex targetVertex) {
		
		if (targetVertex == null) {
			throw new IllegalArgumentException();
		}
		
		assert target.containsIncomingTransition(this);
		
		target.removeIncomingTransition(this);
		target = targetVertex;
		target.addIncomingTransition(this);
		
		assert target.containsIncomingTransition(this);
	}
	
	public void removeBindings() {
		source.removeOutgoingTransition(this);
		target.removeIncomingTransition(this);
	}
	
	public void addBindings() {
		source.addOutgoingTransition(this);
		target.addIncomingTransition(this);
	}
}
